from django.shortcuts import render
from django.shortcuts import redirect
from django.views import generic
from django.contrib.auth import authenticate, login
from django.contrib.auth import views as auth_views
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy
from django.core.mail import send_mail
from django.core.mail import BadHeaderError
from django.template.loader import render_to_string
from django.contrib.auth.models import User
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
from django.contrib.auth.tokens import default_token_generator
from django.http import HttpRequest
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404

from . import forms
from . import models
from . import utils


class AuthorizationPage(auth_views.LoginView):

    form_class = forms.AuthorizationPageForm
    template_name = 'account/login.html'


class LogoutPage(auth_views.LogoutView):

    template_name = 'account/logged_out.html'


@login_required
def show_dashboard(request):

    context = {'section': 'dashboard'}
    return render(request, 'account/dashboard.html', context)


class PasswordChangePage(auth_views.PasswordChangeView):

    form_class = forms.PasswordChangePageForm
    template_name = 'account/password_change.html'
    success_url = reverse_lazy('password_change_done')


class PasswordChangeDonePage(auth_views.PasswordChangeDoneView):

    template_name = 'account/password_change_done.html'


# /PASSWORD CHANGE

# PASSWORD RESET
class PasswordResetCustomView(auth_views.PasswordResetView):

    template_name = 'account/password_reset.html'
    form_class = forms.PasswordResetCustomForm


class PasswordResetDoneCustomView(auth_views.PasswordResetDoneView):

    template_name = 'account/password_reset_done.html'


class PasswordResetConfirmCustomView(auth_views.PasswordResetConfirmView):

    form_class = forms.PasswordResetConfirmCustomForm
    template_name = 'account/password_reset_confirm.html'


class PasswordResetCompleteCustomView(auth_views.PasswordResetCompleteView):

    template_name = 'account/password_reset_complete.html'


# /PASSWORD RESET

class RegistrationPage(generic.View):

    def get(self, request):

        form = forms.RegistrationPageForm
        context = {'form': form}

        return render(request, 'account/registration.html', context)

    def post(self, request):

        bound_form = forms.RegistrationPageForm(request.POST)

        if bound_form.is_valid():

            new_user = bound_form.save(commit=False)
            new_user.set_password(bound_form.cleaned_data['password'])
            new_user.save()
            models.Profile.objects.create(user=new_user)

            return render(request, 'account/registration_done.html')

        else:

            print('error')
            return render(request, 'account/registration.html', {'form': bound_form})


@login_required
def edit(request):

    if request.method == 'POST':

        user_form = forms.UserEdit(instance=request.user,
                                   data=request.POST)

        profile_form = forms.ProfileEdit(instance=request.user.profile,
                                         data=request.POST, 
                                         files=request.FILES)

        if user_form.is_valid() and profile_form.is_valid():

            utils.upload_image(request, profile_form)
            user_form.save()
            profile_form.save()

            return redirect('home_page')

        else:

            print('Wrong!')
            return render(request,
                          'account/user_edit.html',
                          {'user_form': user_form, 'profile_form': profile_form})

    else:

        user_form = forms.UserEdit(instance=request.user)
        profile_form = forms.ProfileEdit(instance=request.user.profile)
        context = {'user_form': user_form, 'profile_form': profile_form, 'user': request.user}

        return render(request, 'account/user_edit.html', context)


class ListUsers(LoginRequiredMixin, generic.View):

    def get(self, request):

        users = User.objects.filter(is_active=True)
        context = {'section': 'people', 'users': users}

        return render(request, 'account/user/list.html', context)


class UserDetail(LoginRequiredMixin, generic.View):

    def get(self, request, username):

        user = get_object_or_404(User, username=username, is_active=True)
        images = user.images_created.all()
        context = {'section': 'people', 'user': user, 'images': images}

        return render(request, 'account/user/detail.html', context)

